from django.db import models
from users.models import BaseModel
from django.contrib.auth.models import Group

# 创建一个系统设定模型
class Setting(BaseModel):
    type_choices = [
        (0, 'Type'),
        (1, 'SettingType')
    ]
    type = models.SmallIntegerField(
        default=1, choices=type_choices, verbose_name='关联模型分类')
    name = models.CharField(max_length=64, db_index=True,
                            verbose_name='设定名称', unique=True)
    title = models.CharField(
        max_length=255, verbose_name='设定名称备注', null=True, blank=True)

    class Meta:
        verbose_name = '设置'
        verbose_name_plural = verbose_name
        # 将name设定查询优化
        index_together = ('name', 'type')

    def __str__(self):
        return self.name
    
class SettingType(BaseModel):
    """
    类型表单，运维人员设定
    """
    setting = models.ForeignKey(
        Setting, on_delete=models.CASCADE, default=None, related_name='setting_type', verbose_name='设定项目')
    name = models.CharField(max_length=64, verbose_name='类型名称', unique=True)
    title = models.CharField(
        max_length=255, verbose_name='类型描述', blank=True, null=True)
    token = models.CharField(
        max_length=255, verbose_name='只写保密字段', blank=True, null=True)

    class Meta:
        verbose_name = '类型设定'
        verbose_name_plural = verbose_name
        # 将name设定查询优化
        index_together = ('name', 'setting')

    def __str__(self):
        return self.name

class Menu(models.Model):
    """
    动态菜单
    """
    path = models.CharField(max_length=64, verbose_name='菜单路径', unique=True)
    name = models.CharField(
        max_length=64, verbose_name='菜单名称', unique=True)
    component = models.CharField(
        max_length=64, verbose_name='组件')
    hidden = models.BooleanField(default=False)
    always_show = models.BooleanField(default=True)
    redirect = models.CharField(
        max_length=64, verbose_name='菜单跳转', blank=True, null=True)
    groups = models.ManyToManyField(
        Group, related_name='menu_group', verbose_name='权限组')
    title = models.CharField(
        max_length=64, verbose_name='菜单描述')
    icon = models.CharField(
        max_length=64, verbose_name='菜单图标', blank=True, null=True)
    breadcrumb = models.BooleanField(default=True)
    active_menu = models.CharField(
        max_length=64, verbose_name='菜单高亮显示', blank=True, null=True)
    menu = models.ForeignKey(
        to='self', on_delete=models.SET_NULL, blank=True, null=True, related_name='up_menu', verbose_name='上级菜单')

    class Meta:
        verbose_name = '动态菜单'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name